//
//  PageTitleView.m
//  PageTitleContent
//
//  Created by 好米有易 on 2017/9/13.
//  Copyright © 2017年 好米有易. All rights reserved.
//

#import "PageTitleView.h"
#import "UIColor+RGB.h"
@interface PageTitleView ()

/** grayLine */
@property (nonatomic, strong) UIView *grayLine;

/** scrollLine */
@property (nonatomic, strong) UIView *scrollLine;

/** 标题栏数组 */
@property (nonatomic, strong) NSMutableArray<UIButton *> *buttons;

/** 当前的按钮 */
@property (nonatomic, strong) UIButton *currentButton;

/** 记录当前按钮索引*/
@property (nonatomic, assign) NSInteger currentIndext;

/** 记录向右侧滚动*/
@property (nonatomic, assign) NSInteger rightDirection;

/** 记录向左滚动*/
@property (nonatomic, assign) NSInteger leftDirection;

/** 红色线条 */
@property (nonatomic, strong) UIView *redLine;

@end



@implementation PageTitleView {
    CGFloat _buttonWidth;
    CGFloat _buttonHeight;
    CGFloat _viewWith;
}


- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollWithProgress:) name:@"ScrollNotification" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recordCurrentBtn:) name:@"RecordCurrentBtn" object:nil];
        
        _buttonHeight = 40;
        _viewWith = self.bounds.size.width;
        self.currentIndext = 0;
        self.rightDirection = 0;
        self.leftDirection = 0;
        [self addSubview:self.grayLine];
    }
    
    return self;
}

#pragma mark - lazy -
- (UIView *)grayLine {
    if (_grayLine == nil) {
        
        _grayLine = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, self.bounds.size.width, 0.5)];
        _grayLine.backgroundColor = [UIColor darkGrayColor];
    }
    return _grayLine;
}

- (UIView *)scrollLine {
    if (_scrollLine == nil) {
        _scrollLine = [[UIView alloc] init];
        _scrollLine.backgroundColor = [UIColor clearColor];
    }
    return _scrollLine;
}

- (UIView *)redLine {
    if (_redLine == nil) {
        _redLine = [[UIView alloc] initWithFrame:CGRectMake(_buttonWidth / 4, 0, _buttonWidth / 2, 2)];
        _redLine.backgroundColor = [UIColor redColor];
    }
    return _redLine;
}




- (NSMutableArray *)buttons {
    if (_buttons == nil) {
        
        _buttons = [NSMutableArray arrayWithCapacity:1];
        
    }
    return _buttons;
}


#pragma mark - UI -

- (void)setupUI {
    
    _buttonWidth = self.bounds.size.width / self.titles.count;
    
    for (int i = 0; i < self.titles.count; i++) {
        
        CGFloat buttonX = i * _buttonWidth;
        CGFloat buttonY = 0;
        
        CGRect frame = CGRectMake(buttonX, buttonY, _buttonWidth, _buttonHeight);
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.frame = frame;
        
        button.tag = i;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [button setTitle:[self.titles objectAtIndex:i] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(clickTitleAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.buttons addObject:button];
        
        [self addSubview:button];
    }
    
    self.scrollLine.frame = CGRectMake(0, self.bounds.size.height - 2.5, _buttonWidth, 2);
    
    [self addSubview:self.scrollLine];
    [self.scrollLine addSubview:self.redLine];
    self.currentButton = self.buttons[0];
    [self.currentButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}

- (void)clickTitleAction:(UIButton *)sender {
    
    
    if (self.currentButton == sender) {
        return;
    }
    
    self.currentIndext = sender.tag;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.scrollLine.frame = CGRectMake(sender.frame.origin.x, self.scrollLine.frame.origin.y, self.scrollLine.frame.size.width, self.scrollLine.frame.size.height);
        
        [self.currentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        self.currentButton = sender;
        
        [self.currentButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        NSDictionary *dict = @{@"index":@(sender.tag)};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ScrollActionNotification" object:nil userInfo:dict];

    }];

    
}



- (void)scrollWithProgress:(NSNotification *)notice {
    
    NSDictionary *dict = [notice valueForKey:@"userInfo"];
    
    CGFloat progress = [dict[@"progress"] floatValue];
    

    CGFloat fabsfProgress = fabs(progress);

    [[self indexWithProgress:progress] setTitleColor:[UIColor colorWithR:255.0 * fabsfProgress g:0 b:0 a:1.0] forState:UIControlStateNormal];

    [self.currentButton setTitleColor:[UIColor colorWithR:255.0 - 255.0 * fabsfProgress g:0 b:0 a:1.0] forState:UIControlStateNormal];
    
    [UIView animateWithDuration:0 animations:^{
        
    self.scrollLine.frame = CGRectMake(self.currentButton.frame.origin.x + _buttonWidth * progress, self.bounds.size.height - 2.5, _buttonWidth, 2);
        
        
    }];

}

- (UIButton *)indexWithProgress:(CGFloat)progress {
    
    NSLog(@"progress -- %@", @(progress));
    if (self.rightDirection == 0 && progress > 0) {
        self.currentIndext +=1;
        self.rightDirection = 1;
        NSLog(@"self.currentIndext -- %@", @(self.currentIndext));
    }
    
    if (self.leftDirection == 0 && progress < 0) {
            self.currentIndext -=1;
            self.leftDirection = 1;
        NSLog(@"self.currentIndext -- %@", @(self.currentIndext));
    }
    
    return self.buttons[self.currentIndext];

    
}

- (void)recordCurrentBtn:(NSNotification *)notice {
    
    NSDictionary *dict = [notice valueForKey:@"userInfo"];

    NSInteger index = [dict[@"index"] integerValue];
    [self.currentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.currentButton = self.buttons[index];
    self.currentIndext = index;
    self.leftDirection = 0;
    self.rightDirection = 0;
    [self.currentButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

}





#pragma mark - setter -
- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    [self setupUI];
}


@end
